/*
 * @(#)Config.java 1.0 2012-9-10
 *
 * Copyright @2011-2011 Gopay Information and Technology Co., Ltd.
 * Sanyunqiao, Chaoyang, Beijing, 100027, China.
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Gopay Information and technology Co., Ltd. ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with Gopay.
 */
package com.gopay.cashier.web.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 系统配置类。
 * 
 * 
 * @author zhanglei
 * @version 1.0 2012-9-14
 */
public class CashierConfig {
    protected final static Logger logger = LoggerFactory.getLogger(CashierConfig.class);
    /** 是否禁用图片验证码 true:是；false:否 */
    public static final String IS_CAPTCHA_DISABLED = "captcha.disabled";
    /** CPS的dispatch地址 */
    public static final String CPS_DISPATCH_URL = "cps.dispatch.url";
    /** BIS的地址 */
    public static final String BIS_URL = "bis.url";
    /** 支付完成后，cashier返回到cps的前台页面地址 */
    public static final String CASHIER_CALLBACK_REDIRECT_URL = "cashier.callback.redirect.url";
    /** 充值交易portal发起页面 */
    public static final String CASHIER_INCHARGE_REDIRECT_INDEX = "portal.incharge.index";
    /** 充值交易支付完成后，cashier返回到portal的前台页面地址 */
    public static final String CASHIER_INCHARGE_REDIRECT_URL = "portal.incharge.url";
    /** 是否禁用SQL注入拦截器 */
    public static final String INJECTION_INTERCEPTOR_DISABLED = "injectionInterceptor.disabled";
    /** 国付宝首页 */
    public static final String LINK_HOMEPAGE = "link.homepage";
    /** 手机版国付宝 */
    public static final String LINK_MOBILE = "link.mobile";
    /** 国付宝微博 */
    public static final String LINK_WEIBO_SINA = "link.weibo.sina";
    /** 帮助中心 */
    public static final String LINK_HELPCENTER = "link.helpcenter";
    /** 反钓鱼-商户LOGO图片服务器地址：http://img.gopay.com.cn/merchanLogo/ */
    public static final String MER_LOGO_URL = "merchanLogo.url";
    
    /**
     * 收银台地址
     */
    public static final String CASHIER_URL ="cashier.url";
    /**
     * 单点登录服务器地址
     */
    public static final String CAS_SERVER_URL ="cas.server.url";
    
    /**
     * Portal地址
     */
    public static final String PORTAL_URL ="portal.url";
    public static final String CLOUD_URL ="cloudat.url";
    /**
     * 是否启用指纹验证 0：不启用  1：启用  
     */
    public static final String ENABLE_FINGERINFO = "enable_fingerinfo";
    
    /** G商融通充值完成，cashier返回到商户的前台页面地址 */
    public static final String CASHIER_CALLBACK_GSRT_URL = "cashier.callback.gsrt.url";
    
    /** 跨境人民币汇款结果 **/
    public static final String CASHIER_REMIT_REDIRECT_URL = "cbrmb.portal.remit.url";
    
    /** 跨境人民币进口网关批量支付结果 **/
    public static final String CASHIER_BATCHPAY_REDIRECT_URL = "cbrmb.portal.batchpay.url";
    
    private static Map<String, String> configMap = null;
    private static Object LOCK = new Object();
    private static final String[] FILES = { "cashier.properties" };

    public CashierConfig() throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    public static String get(String key) {
        init();
        return configMap.get(key);
    }

    public static int getInt(String key) {
        init();
        String strValue = configMap.get(key);
        try {
            return Integer.parseInt(strValue);
        } catch (Exception e) {
            logger.error("getInt parsing error:[" + key + "][" + strValue + "]", e);
            throw new RuntimeException(e);
        }
    }

    public static long getLong(String key) {
        init();
        String strValue = configMap.get(key);
        try {
            return Long.parseLong(strValue);
        } catch (Exception e) {
            logger.error("getLong parsing error:[" + key + "][" + strValue + "]", e);
            throw new RuntimeException(e);
        }
    }

    public static boolean getBool(String key) {
        init();
        String strValue = configMap.get(key);
        try {
            return Boolean.parseBoolean(strValue);
        } catch (Exception e) {
            logger.error("getBool parsing error:[" + key + "][" + strValue + "]", e);
            throw new RuntimeException(e);
        }
    }

    private static void init() {
        if (null == configMap) {
            synchronized (LOCK) {
                if (null == configMap) {// 遍历加载所有配置文件
                    configMap = new HashMap<String, String>();
                    Properties propFile = null;
                    for (int i = 0; i < FILES.length; i++) {// 加载一个配置文件
                        InputStream inputStream = null;
                        try {
                            propFile = new Properties();
                            inputStream = CashierConfig.class.getClassLoader().getResourceAsStream(FILES[i]);
                            propFile.load(inputStream);
                            Iterator<Map.Entry<Object, Object>> it = propFile.entrySet().iterator();
                            while (it.hasNext()) {
                                Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) it.next();
                                String key = (String) entry.getKey();
                                String value = (String) entry.getValue();
                                configMap.put(key, StringUtils.trimToEmpty(value));
                            }
                        } catch (Exception e) {
                            logger.error("加载配置文件[" + FILES[i] + "]出错！", e);
                        } finally {
                            try {
                                if(inputStream!=null)
                                    inputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }// 加载一个配置文件结束
                }// 遍历加载所有配置文件结束
            }
        }
        return;
    }
}
